import Vue from 'vue'
import {
	baseURL
} from "@/api/config.js";
console.log('baseURL', baseURL)
const request = (params) => {
	// 显示加载数据的提示
	uni.showLoading({
		title: 'loading...',
		mask: true
	})
	return new Promise(function(resolve, reject) {
		let token = uni.getStorageSync('userToken') || '419eb2b4f8a1384e7dd3d6efc4410e24';
		if (params.noVerify) {
			token = ''
		}
		uni.request({
			method: params.method || 'GET',
			data: params.data,
			url: baseURL + params.url,
			header: {
				'token': token,
			},
			success(res) {
				uni.hideLoading()
				// console.log('res',res)
				if (res.data.code == 401) {
					uni.showToast({
						title: '登录失效，请重新登录！',
						icon: "none",
						mask: true
					})
					setTimeout(() => {
						// uni.clearStorageSync()
						// uni.reLaunch({
						// 	url: "/pages/login/login"
						// })
					}, 1000)
				} else if (res.statusCode == 506) {
					uni.showToast({
						title: '该用户已被禁用',
						icon: "none",
						mask: true
					})
					return

				} else if (res.data.code == 422 || res.data.code == 500 || res.data.code == 0) {
					//报错
					uni.showToast({
						title: res.data.msg || '系统未知错误，请反馈给管理员',
						icon: 'none',
						duration: 2000
					});
					// console.log('res.data.msg',res.data.msg)
					return
				}
				resolve(res.data)
			},
			fail(err) {
				uni.hideLoading()
				uni.showToast({
					title: err.data.msg || '系统未知错误，请反馈给管理员',
					icon: 'none',
					duration: 2000
				});
				reject(err)

			},
			complete(e) {
				uni.hideLoading()
			}
		})
	})

}
export default request;